package com.caiyi.tips.core.servlet;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

public class RandCodeServlet extends HttpServlet {

	private static final long serialVersionUID = -6316706844836398723L;

	private void processRequest(HttpServletRequest paramHttpServletRequest,
			HttpServletResponse paramHttpServletResponse)
			throws ServletException, IOException {

		paramHttpServletResponse.setContentType("image/jpeg");
		paramHttpServletResponse.setHeader("Pragma", "no-cache");
		paramHttpServletResponse.setHeader("Cache-Control", "no-cache");
		paramHttpServletResponse.setDateHeader("Expires", 0);
		int i = 60;
		int j = 20;
		BufferedImage localBufferedImage = new BufferedImage(i, j, 1);
		Graphics localGraphics = localBufferedImage.getGraphics();
		Random localRandom = new Random();
		String str1 = paramHttpServletRequest.getParameter("sid");
		if (str1 == null)
			str1 = "rand";
		localGraphics.setColor(_$1(200, 250));
		localGraphics.fillRect(0, 0, i, j);
		localGraphics.setFont(new Font("Times New Roman", 0, 18));
		localGraphics.setColor(_$1(160, 200));
		for (int k = 0; k < 155; ++k) {
			int l = localRandom.nextInt(i);
			int i1 = localRandom.nextInt(j);
			int i2 = localRandom.nextInt(12);
			int i3 = localRandom.nextInt(12);
			localGraphics.drawLine(l, i1, l + i2, i1 + i3);
		}
		String str2 = "";
		for (int l = 0; l < 4; ++l) {
			String str3 = String.valueOf(localRandom.nextInt(10));
			str2 = str2 + str3;
			localGraphics.setColor(new Color(20 + localRandom.nextInt(110),
					20 + localRandom.nextInt(110), 20 + localRandom
							.nextInt(110)));
			localGraphics.drawString(str3, 13 * l + 6, 16);
		}
		HttpSession session = paramHttpServletRequest.getSession(true);
		if (session.isNew()) {
			session.setMaxInactiveInterval(300);
		}
		session.setAttribute(str1, str2.toLowerCase());
		localGraphics.dispose();
		localBufferedImage.flush();
		ServletOutputStream localServletOutputStream = paramHttpServletResponse
				.getOutputStream();
		ImageIO.write(localBufferedImage, "JPEG", localServletOutputStream);
		localServletOutputStream.flush();
		localServletOutputStream.close();
	}

	private Color _$1(int paramInt1, int paramInt2) {
		Random localRandom = new Random();
		if (paramInt1 > 255)
			paramInt1 = 255;
		if (paramInt2 > 255)
			paramInt2 = 255;
		int i = paramInt1 + localRandom.nextInt(paramInt2 - paramInt1);
		int j = paramInt1 + localRandom.nextInt(paramInt2 - paramInt1);
		int k = paramInt1 + localRandom.nextInt(paramInt2 - paramInt1);
		return new Color(i, j, k);
	}

	protected void doGet(HttpServletRequest paramHttpServletRequest,
			HttpServletResponse paramHttpServletResponse)
			throws ServletException, IOException {
		processRequest(paramHttpServletRequest, paramHttpServletResponse);
	}

	protected void doPost(HttpServletRequest paramHttpServletRequest,
			HttpServletResponse paramHttpServletResponse)
			throws ServletException, IOException {
		processRequest(paramHttpServletRequest, paramHttpServletResponse);
	}

	public String getServletInfo() {
		return "Short description";
	}
}
